package com.smstransmit

import android.Manifest
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import com.smstransmit.control.SmsControl
import com.smstransmit.service.SmsService
import com.xmrecruit.permission.PermissionUtil
import com.xmrecruit.util.PreferenceUtil
import kotlinx.android.synthetic.main.activity_main.*
import android.content.ComponentName
import com.smstransmit.service.SmsService.LocalBinder
import android.os.IBinder
import android.content.ServiceConnection
import android.text.Editable
import android.text.TextUtils
import android.widget.Toast
import com.smstransmit.reciver.SmsReciver
import android.content.IntentFilter




class MainActivity : Activity() {
    var mService: SmsService? = null

    val permission = arrayOf(Manifest.permission.RECEIVE_SMS)
    val permissionMessage = arrayOf(PermissionUtil.PERMISSION_SMS)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

//        smtp_edittext.text = Editable.Factory.getInstance().newEditable(PreferenceUtil.getString(SmsControl.smtp, ""))
//        smtp_port_edittext.text = Editable.Factory.getInstance().newEditable(PreferenceUtil.getString(SmsControl.smtp_port, ""))
//        send_address_edittext.text = Editable.Factory.getInstance().newEditable(PreferenceUtil.getString(SmsControl.send_address, ""))
//        send_password_edittext.text = Editable.Factory.getInstance().newEditable(PreferenceUtil.getString(SmsControl.send_password, ""))
//        receive_address_edittext.text = Editable.Factory.getInstance().newEditable(PreferenceUtil.getString(SmsControl.receive_address, ""))

        PermissionUtil.CheckSelfPermission(this@MainActivity, permission, permissionMessage)
        val intent = Intent(this, SmsService::class.java)
        startService(intent)
        btn_send.setOnClickListener {
            SmsControl.send(this@MainActivity)
        }
        btn_send_Bro.setOnClickListener {
            val intent = Intent()
            intent.action = "android.provider.Telephony.SMS_RECEIVED"
            sendBroadcast(intent)
        }
        btn_save.setOnClickListener {
            var smtp = smtp_edittext.text.toString()
            var smtp_port = smtp_port_edittext.text.toString()
            var send_address = send_address_edittext.text.toString()
            var send_password = send_password_edittext.text.toString()
            var receive_address = receive_address_edittext.text.toString()
            if (TextUtils.isEmpty(smtp)
                    || TextUtils.isEmpty(smtp_port)
                    || TextUtils.isEmpty(send_address)
                    || TextUtils.isEmpty(send_password)
                    || TextUtils.isEmpty(receive_address)
                    ) {
                Toast.makeText(this, "请填写完整配置信息", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            PreferenceUtil.saveString(SmsControl.smtp, smtp)
            PreferenceUtil.saveString(SmsControl.smtp_port, smtp_port)
            PreferenceUtil.saveString(SmsControl.send_address, send_address)
            PreferenceUtil.saveString(SmsControl.send_password, send_password)
            PreferenceUtil.saveString(SmsControl.receive_address, receive_address)
        }
    }

    private val mConnection = object : ServiceConnection {

        override fun onServiceConnected(className: ComponentName,
                                        service: IBinder) {
            // We've bound to LocalService, cast the IBinder and get LocalService instance
            val binder = service as LocalBinder
            mService = binder.getService()
        }

        override fun onServiceDisconnected(arg0: ComponentName) {
        }
    }
}
